package com.huajx.tuopuda.server.udp;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-17 20:07
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.huajx.core.util.MessageKit;

public class TestUdpSocketServer extends Thread {
    private ServerSocket server;
    private BufferedReader reader;
    private JTextArea jt = new JTextArea(15, 20);
    private JTextField jf = new JTextField();
    private String word;
    private byte[] buf;
    private int port = 60000;
    private MulticastSocket socket = null;
    private InetAddress iaddress = null;

    public TestUdpSocketServer() {
        init();

        jf.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                word = jf.getText();
                buf = word.getBytes();
                try {
                    iaddress = InetAddress.getByName("224.0.0.5");
                    socket = new MulticastSocket(port);
                    socket.setTimeToLive(1);
                    socket.joinGroup(iaddress);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                jt.append("广播站开始广播！！！\n");
                start();
            }
        });
    }

    public void init() {
        JFrame frame = new JFrame("Socket");
        JScrollPane js = new JScrollPane(jt);
        Container con = frame.getContentPane();
        frame.setLocationRelativeTo(null);
        con.add(js, "North");
        con.add(jf, "South");

        frame.setSize(400, 400);
        frame.setLocation(0,0);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public void run(){
    	byte[] receiveBytes = new byte[34];
    	DatagramPacket packet = new DatagramPacket(receiveBytes, receiveBytes.length);
    	while(true){
    		try {
				socket.receive(packet);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

            InetAddress clientAddress = packet.getAddress();
            int clientPort = packet.getPort();

//            jt.append(clientAddress.getHostAddress());
            jt.append(clientAddress.toString()+":");
            jt.append(clientPort + ">>");

    		jt.append(MessageKit.bytesToHex(receiveBytes));
    		jt.append("\n");

//    		socket.send();
    	}
    }

//    public void run() {
//        while (true) {
//            buf = word.getBytes();
//            DatagramPacket packet = new DatagramPacket(buf, buf.length, iaddress, port);
//            jt.append(word + "\n");
//            try {
//                socket.send(packet);
//                sleep(3000);
//            } catch (Exception e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
//    }

    public static void main(String[] args) {
        TestUdpSocketServer sS = new TestUdpSocketServer();
    }

}